﻿Imports Negocio
Imports Servicios
Imports Excepciones

Public Class frmConsultaUsuario

    Protected Overrides Sub PreLoad()
        dgvUsuarios.AutoGenerateColumns = False
        Me.Icon = Icon.FromHandle(Global.SIPRO.My.Resources.Resources.People.GetHicon())
    End Sub

    Private Sub frmConsultaBitacora_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Init()
    End Sub

    Sub Init()
        Try
            Dim usuarios As New List(Of Usuario)
            usuarios = GesUsuarios.Consultar(txtNick.Text, txtDNI.Text, txtNOMBRE.Text, txtAPELLIDO.Text)
            dgvUsuarios.DataSource = usuarios
        Catch ex As Exception
            My.Application.HandlerException(ex)
            Me.Close()
        End Try
    End Sub

    Private Sub btnBuscar_Click(sender As System.Object, e As System.EventArgs) Handles btnBuscar.Click
        If MyBase.ValidateForm() Then
            Init()
        End If
    End Sub

    Private Sub btnCancel_Click(sender As System.Object, e As System.EventArgs) Handles btnCerrar.Click
        Me.Close()
    End Sub

    Private Sub btnEliminar_Click(sender As System.Object, e As System.EventArgs) Handles btnEliminar.Click
        If dgvUsuarios.Rows.Count > 0 AndAlso TypeOf dgvUsuarios.CurrentRow.DataBoundItem Is Usuario Then
            If MessageExt.Show(GesIdiomas.TraduceMensaje("MSGU002").ToString, MessageBoxButtons.OKCancel) = DialogResult.OK Then
                Dim usu As Usuario = DirectCast(dgvUsuarios.CurrentRow.DataBoundItem, Usuario)
                Try
                    GesUsuarios.Eliminar(usu)
                Catch ex As Exception
                    My.Application.HandlerException(ex)
                End Try
                Init()
            End If
        Else
            My.Application.HandlerException(New ItemSelectRequiredException())
        End If
    End Sub

    Private Sub btnNuevo_Click(sender As System.Object, e As System.EventArgs) Handles btnNuevo.Click
        Dim f = New frmABMUsuario
        If f.ShowDialog = DialogResult.OK Then
            Init()
        End If
    End Sub

    Private Sub dgvRecursos_CellMouseDoubleClick(sender As System.Object, e As System.EventArgs) Handles dgvUsuarios.DoubleClick
        If dgvUsuarios.Rows.Count > 0 AndAlso TypeOf dgvUsuarios.CurrentRow.DataBoundItem Is Usuario Then
            Dim usu As Usuario = DirectCast(dgvUsuarios.CurrentRow.DataBoundItem, Usuario)
            Dim f As New frmABMUsuario(usu)
            If f.ShowDialog = DialogResult.OK Then
                Init()
            End If
        Else
            My.Application.HandlerException(New ItemSelectRequiredException())
        End If
    End Sub
End Class